package net.zjitc.service.impl;

import net.zjitc.common.ResponseResult;
import net.zjitc.entity.reports.*;
import net.zjitc.exception.ReportsException;
import net.zjitc.mapper.ReportsOneMapper;
import net.zjitc.service.ReportsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 毛钰深
 * @create 2022/2/6
 */
@Service
@Transactional
public class IReportsService implements ReportsService {

    @Autowired
    private ReportsOneMapper reportsOneMapper;

    @Override
    public ResponseResult getReportsOne() throws ReportsException {
        ResponseResult res=new ResponseResult();
        List<ReportsSql> reportsSqls = reportsOneMapper.selectList(null);
        if (reportsSqls==null){
            throw new ReportsException("查询的列表为空");
        }
        List<String> names=new ArrayList<>();
        List<Date> dates=new ArrayList<>();
        List<Series> series=new ArrayList<>();
        for (ReportsSql reportsSql : reportsSqls) {
            if (!names.contains(reportsSql.getRp1_area())){
                names.add(reportsSql.getRp1_area());
            }
            if (!dates.contains(reportsSql.getRp1_date())){
                dates.add(reportsSql.getRp1_date());
            }

            String rpArea = reportsSql.getRp1_area();
            boolean flag=true;
            for (Series series1 : series) {
                if (series1.getName().equals(rpArea)){
                    series1.getData().add(reportsSql.getRp1_user_count());
                    flag=false;
                }
            }
            if (flag){
                Series series1 = new Series(rpArea,"line","总量",new AreaStyle(),new ArrayList<>());
                series1.getData().add(reportsSql.getRp1_user_count());
                series.add(series1);
            }
        }

        ReportsOne reportsOne=new ReportsOne();
        reportsOne.setLegend(new Common<>());
        reportsOne.getLegend().setData(names);

        reportsOne.setYAxis(new ArrayList<>());
        reportsOne.getYAxis().add(new CommonType());

        reportsOne.setXAxis(new ArrayList<>());
        Common<Date> common= new Common<>();
        common.setData(dates);
        reportsOne.getXAxis().add(common);

        reportsOne.setSeries(series);


        res.setSuccess("获取报表成功",reportsOne);
        return res;
    }
}
